#ifndef SMSSOCKETREADER_H
#define SMSSOCKETREADER_H

// INCLUDES 
#include <e32base.h>
#include <es_sock.h>                    // RSocketServ
#include <f32file.h>                    // RFs

// LIBS
// esock.lib (RSocketServ), smsu.lib (TSmsAddr), gsmu.lib (CSmsBuffer),
// efsrv.lib (RFs), estor.lib (RSmsSocketReadStream)

// CAPS
// NetworkServices (RSocket::Bind), ReadUserData (RSocket::Bind), 
// WriteUserData (RSocket::Bind)

// FORWARD DECLARATIONS
class MSmsSocketReaderObserver;

// CLASS DECLARATION
/**
 * CSmsSocketReader class.
 * CSmsSocketReader declaration.
 * 
 */
class CSmsSocketReader : public CActive
    {
    public: // Constructors and destructor

        /**
         * NewL()
         * Creates new CSmsSocketReader object.
         * @param Reference to the MSmsSocketReaderObserver object.
         * @return Pointer to the created instance of CSmsSocketReader.
         */
        static CSmsSocketReader* NewL( MSmsSocketReaderObserver & aObserver);
    
        /**
         * NewLC()
         * Creates new CSmsSocketReader object.
         * @param Reference to the MSmsSocketReaderObserver object.
         * @return Pointer to the created instance of CSmsSocketReader.
         */
        static CSmsSocketReader* NewLC( MSmsSocketReaderObserver & aObserver );
    
        /**
         * ~CSmsSocketReader()
         * Destructor.
         */
        ~CSmsSocketReader();
    
    protected: // From CActive
    
        /**
         * DoCancel()
         * Implements cancellation of an outstanding request.
         */
        void DoCancel();
    
        /**
         * RunL()
         * Handles an active objects request completion event.
         */
        void RunL();
    
        /**
         * RunError()
         * Handles a leave occurring in the request completion event handler RunL().
         * @param aError The leave code.
         * @return KErrNone if error was handled, otherwise system-wide error.
         */
        TInt RunError( TInt aError );
    
    private: // New functions
            
        /**
         * Start()
         * Starts waiting for the actual socket data.
         */
        void Start();

    public: // New functions
        
        /**
         * StartListeningL()
         * Starts listening for an incoming SMS.
         * @param aMatchTag Match tag to listen to. Default - catch all messages.
         */
        void StartListeningL( const TDesC8& aMatchTag = KNullDesC8 );
        
        /**
         * StopListening()
         * Stops listening.
         */
        void StopListening();

    private: // Constructors
    
        /**
         * CSmsSocketReader()
         * Default C++ constructor.
         * @param Reference to the MSmsSocketReaderObserver object.
         */
        CSmsSocketReader( MSmsSocketReaderObserver & aObserver );
    
        /**
         * ConstructL()
         * Default EPOC constructor.
         */
        void ConstructL();
        
    private: // enum
        
        enum TSmsSocketEngineState
            {
            ESmsIdle,
            ESmsListening,
            ESmsSystemNotyfing
            };
        
    private: // data
    
        MSmsSocketReaderObserver&   iObserver;
        RSocketServ                 iSocketServ;
        RSocket                     iReadSocket;
        
        RFs                         iFs;
        
        TBool                       iWait;
        TPckgBuf<TUint>             iBuf;
        
        TSmsSocketEngineState       iState;
    };

#endif // SMSSOCKETREADER_H
